﻿package com.msjt.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.struts2.ServletActionContext;

import com.msjt.base.BaseAction;
import com.msjt.entity.Picture;
import com.msjt.service.IPictureService;
import com.msjt.util.DeleteFile;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;

/**
 * name:PictureAction 
 * function:图片模块Action 
 * author: kw 
 * time:2013.8.28
 * updateTime:2013.8.30
 */
@SuppressWarnings("serial")
public class PictureAction extends BaseAction {
	private Picture picture;
	@Resource
	private IPictureService pictureService;
	private PageMode page;
	// 文件上传
	// 文件上传
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;

	public PageMode getPage() {
		return page;
	}

	public void setPage(PageMode page) {
		this.page = page;
	}

	public Picture getPicture() {
		return picture;
	}

	public void setPicture(Picture picture) {
		this.picture = picture;
	}

	public IPictureService getPictureService() {
		return pictureService;
	}

	public void setPictureService(IPictureService pictureService) {
		this.pictureService = pictureService;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	// 分页
	public void splitPage(String actionPath) {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(12);
		}
		this.page.setAction(actionPath);
		this.page.setModelDrive("page");
		// 总数量
		page.setSumRows(this.pictureService.getPictureCount());
		page.createPageStr();
		// 分页后的结果
		this.getRequest().put(
				"pictureList",
				this.pictureService.getPicturePage(this.page.getPageIndex(),
						this.page.getPageSize()));
		// 分页后的链接
		this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
	}

	// 前台查看图片列表，分页
	public String showPicture() throws Exception {
		this.splitPage("picture_showPicture.action");
		return "more";
	}

	// 后台查看图片列表，分页
	public String showPictureM() throws Exception {
		this.splitPage("picture_showPictureM.action");
		return "admin";
	}

	// 新上传图片
	public String addPicture() throws Exception {
		// 上传图片begin
		// 上传文件
		// 得到工程保存图片的路径
		String realpath = ServletActionContext.getServletContext().getRealPath("/uploadImages");
		// 用时间来命名
		Date date = new Date();
		// 循环上传的文件
		String name = "";
		for (int i = 0; i < file.size(); i++) {
			// 存入文件名
			name = date.getTime() + (new Random()).nextInt(10000)
					+ fileFileName.get(i);
			InputStream is = new FileInputStream(file.get(i));
			// 得到图片保存的位置(根据realpath来得到图片保存的路径在tomcat下的该工程里)
			File destFile = new File(realpath, name);
			// 把图片写入到上面设置的路径里
			OutputStream os = new FileOutputStream(destFile);			
			byte[] buffer = new byte[400];
			int length = 0;
			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			is.close();
			os.close();
			//生成缩略图
			//Thumbnails.of(destFile).scale(0.50f).toFile(destFile);
			Thumbnails.of(destFile).size(600, 500).toFile(destFile);  
		}
		String path = "uploadImages/" + name;
		path = path.replace("\\", "/");
		// 上传图片end
		// 图片上传时间
		this.picture.setPicPath(path);
		this.picture.setPicDate(new MyDate().getDateString());
		boolean flag = this.pictureService.addPicture(this.picture);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('上传图片失败！');history.go(-1);</script>");
			return "error";
		}
		this.file=null;
		this.fileContentType=null;
		this.fileFileName=null;
		return showPictureM();
	}

	// 准备修改
	public String preUpdate() throws Exception {
		Picture pic = this.pictureService.getPictureInfo(this.picture);
		this.getRequest().put("pic", pic);
		return "update";
	}

	// 修改图片信息
	public String updatePicture() throws Exception {
		System.gc();
		String name = "";
		if (file!= null&&file.size()!=0) {
			// 上传文件
			// 得到工程保存图片的路径
			String path = ServletActionContext.getServletContext().getRealPath("");
			// 循环上传的文件
			// 删除原文件
			// 还需要删除服务器上的图片，以免服务器项目臃肿
			String picture = path + "/" + this.picture.getPicPath();
			picture = picture.replace("\\", "/");
			// 删除服务器的图片
			DeleteFile dFile = new DeleteFile();
			dFile.deleteFolder(picture);
			Date date = new Date();
			String realpath = ServletActionContext.getServletContext()
					.getRealPath("/uploadImages");
			// 循环上传的文件
			for (int i = 0; i < file.size(); i++) {
				// 存入文件名
				name = date.getTime() + (new Random()).nextInt(10000)
						+ fileFileName.get(i);
				InputStream is = new FileInputStream(file.get(i));
				// 得到图片保存的位置(根据realpath来得到图片保存的路径在tomcat下的该工程里)
				File destFile = new File(realpath, name);
				// 把图片写入到上面设置的路径里
				OutputStream os = new FileOutputStream(destFile);
				byte[] buffer = new byte[400];
				int length = 0;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}
				is.close();
				os.close();
				//生成缩略图
				//Thumbnails.of(destFile).scale(0.50f).toFile(destFile);
				Thumbnails.of(destFile).size(600, 500).toFile(destFile); 
			}
			String spath = "uploadImages/" + name;
			spath = spath.replace("\\", "/");
			this.picture.setPicPath(spath);
		}
		boolean flag = this.pictureService.updatePicture(this.picture);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		this.file=null;
		this.fileContentType=null;
		this.fileFileName=null;
		return showPictureM();
	}

	// 删除图片
	public String deletePicture() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("");
		// 还需要删除服务器上的图片，以免服务器项目臃肿
		Picture pic = this.pictureService.getPictureInfo(this.picture);
		String picture = path + "/" + pic.getPicPath();
		picture = picture.replace("\\", "/");
		// 删除服务器的图片
		DeleteFile dFile = new DeleteFile();
		dFile.deleteFolder(picture);
		// 删除图片结束
		boolean flag = this.pictureService.deletePicture(this.picture);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除图片失败！');history.go(-1);</script>");
			return "error";
		}
		return showPictureM();
	}

	// 根据ID得到图片的详细信息
	public String showDetail() throws Exception {
		Picture pic = this.pictureService.getPictureInfo(this.picture);
		this.getRequest().put("pic", pic);
		return "showDetail";
	}

	// 批量删除图片，还需要删除本地图片
	public String deleteMore() throws Exception {
		String idString = ServletActionContext.getRequest().getParameter("idString");
		idString = idString.substring(0, idString.length() - 1);
		List<String> pictureList=this.pictureService.getPicturePath(idString);
		String path = ServletActionContext.getServletContext().getRealPath("");
		DeleteFile dFile = new DeleteFile();
		if (pictureList!=null) {
			for (int i = 0; i < pictureList.size(); i++) {
				// 还需要删除服务器上的图片，以免服务器项目臃肿
				String picture = path + "/" + pictureList.get(i);
				picture = picture.replace("\\", "/");
				// 删除服务器的图片			
				dFile.deleteFolder(picture);
			}
		}
		
		boolean flag = this.pictureService.deleteMore(idString);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return showPictureM();
	}
	
	//批量 新上传图片
	public String addMorePicture() throws Exception {
		// 上传图片begin
		// 上传文件
		// 得到工程保存图片的路径
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/uploadImages");
		// 用时间来命名
		Date date = new Date();
		// 循环上传的文件
		String name = "";
		List<String> pathList=new ArrayList<String>();
		for (int i = 0; i < file.size(); i++) {
			// 存入文件名
			name = date.getTime() + (new Random()).nextInt(10000)
					+ fileFileName.get(i);
			InputStream is = new FileInputStream(file.get(i));
			// 得到图片保存的位置(根据realpath来得到图片保存的路径在tomcat下的该工程里)
			File destFile = new File(realpath, name);
			// 把图片写入到上面设置的路径里
			OutputStream os = new FileOutputStream(destFile);
			byte[] buffer = new byte[400];
			int length = 0;
			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			is.close();
			os.close();
			//生成缩略图
			//Thumbnails.of(destFile).scale(0.50f).toFile(destFile);
			Thumbnails.of(destFile).size(600, 500).toFile(destFile); 
			String path = "uploadImages/" + name;
			path = path.replace("\\", "/");
			pathList.add(path);
		}
		// 上传图片end
		// 图片上传时间
		if(this.picture==null){
			this.picture=new Picture();
		}
		for (int i = 0; i < pathList.size(); i++) {
			this.picture.setPicPath(pathList.get(i));
			this.picture.setPicDesc("暂无描述");
			this.picture.setPicDate(new MyDate().getDateString());
			boolean flag = this.pictureService.addPicture(this.picture);
			if (!flag) {
				this.getRequest().put("msg",
						"<script>alert('上传图片失败！');history.go(-1);</script>");
				return "error";
			}
		}
		this.file=null;
		this.fileContentType=null;
		this.fileFileName=null;
		return SUCCESS;
	}
	
}
